/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * UpdateSched.java
 *
 * Created on Jul 27, 2012, 11:38:24 AM
 */
package monitor;

import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.sql.SQLException;
import javax.swing.JOptionPane;

/**
 *
 * @author owner
 */
public class UpdateSched extends javax.swing.JFrame {

    /** Creates new form UpdateSched */
    
    String SchedID;
    String mode;
    String tutorID;
    String tuteeID;
    String mode2;
    static String[] day = {"Monday","Tuesday","Wednesday","Thursday","Friday", "Sunday"};
    static String[] time = {"0800-0930", "0940-1110", "1120-1250", "1300-1430", "1440-1610", "1620-1750"};

    boolean flag = false;
    String course;
    
    private Component frame;
    private Container c;
    
    public UpdateSched() {
        initComponents();
        mode = "Sort";
        mode2 = "Label";
        c = getContentPane();
        c.setBackground(Color.white);
     
      
        
        tutorIdList.removeAllItems();
        tuteeIdList.removeAllItems();
      
        
        QHandler queryHandler = new QHandler();
        DBManager dbconnector = new DBManager();
        dbconnector.Connect();
        queryHandler.selectIdNumTutee();
        dbconnector.PullID(queryHandler.query, mode);
        dbconnector.Disconnect(dbconnector.rs);
        
        QHandler queryHandler1 = new QHandler();
        DBManager dbconnector1 = new DBManager();
        dbconnector1.Connect();
        queryHandler1.selectIdNumTutor();
        dbconnector1.PullInfo(queryHandler1.query, mode);
        dbconnector1.Disconnect(dbconnector1.rs);
        
         
        
         
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        schedLabel = new javax.swing.JLabel();
        tutorNameLabel = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        tuteeNameLabel = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        jLabel10 = new javax.swing.JLabel();
        jLabel11 = new javax.swing.JLabel();
        jLabel12 = new javax.swing.JLabel();
        courseTxt = new javax.swing.JTextField();
        jLabel13 = new javax.swing.JLabel();
        BtnCancel = new javax.swing.JButton();
        BtnUpdate = new javax.swing.JButton();
        tutorIdList = new javax.swing.JComboBox();
        BtnRefresh = new javax.swing.JButton();
        tuteeIdList = new javax.swing.JComboBox();
        schedDay = new javax.swing.JComboBox();
        schedTime = new javax.swing.JComboBox();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Update Schedule");

        jLabel1.setBackground(new java.awt.Color(255, 255, 255));
        jLabel1.setText("Schedule ID");

        schedLabel.setBackground(new java.awt.Color(255, 255, 255));
        schedLabel.setFont(new java.awt.Font("Dialog", 2, 12)); // NOI18N
        schedLabel.setText(" ");

        tutorNameLabel.setBackground(new java.awt.Color(255, 255, 255));
        tutorNameLabel.setFont(new java.awt.Font("Dialog", 1, 12)); // NOI18N
        tutorNameLabel.setText("  ");

        jLabel6.setBackground(new java.awt.Color(255, 255, 255));
        jLabel6.setText("Tutoring Schedule Record");

        tuteeNameLabel.setBackground(new java.awt.Color(255, 255, 255));
        tuteeNameLabel.setFont(new java.awt.Font("Dialog", 1, 12)); // NOI18N
        tuteeNameLabel.setText("  ");

        jLabel8.setBackground(new java.awt.Color(255, 255, 255));
        jLabel8.setText("Tutor ID");

        jLabel9.setBackground(new java.awt.Color(255, 255, 255));
        jLabel9.setText("Tutor Name");

        jLabel10.setBackground(new java.awt.Color(255, 255, 255));
        jLabel10.setText("Tutee ID");

        jLabel11.setBackground(new java.awt.Color(255, 255, 255));
        jLabel11.setText("Tutee Name");

        jLabel12.setBackground(new java.awt.Color(255, 255, 255));
        jLabel12.setText("Course Tutored");

        jLabel13.setBackground(new java.awt.Color(255, 255, 255));
        jLabel13.setText("Schedule");

        BtnCancel.setFont(new java.awt.Font("Modern No. 20", 0, 18)); // NOI18N
        BtnCancel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/pictures/Cancel.png"))); // NOI18N
        BtnCancel.setText("Cancel");
        BtnCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BtnCancelActionPerformed(evt);
            }
        });

        BtnUpdate.setFont(new java.awt.Font("Modern No. 20", 0, 18)); // NOI18N
        BtnUpdate.setIcon(new javax.swing.ImageIcon(getClass().getResource("/pictures/update.png"))); // NOI18N
        BtnUpdate.setText("Update");
        BtnUpdate.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BtnUpdateActionPerformed(evt);
            }
        });

        tutorIdList.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        BtnRefresh.setFont(new java.awt.Font("Modern No. 20", 0, 18)); // NOI18N
        BtnRefresh.setIcon(new javax.swing.ImageIcon(getClass().getResource("/pictures/refresh.png"))); // NOI18N
        BtnRefresh.setText("Refresh");
        BtnRefresh.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BtnRefreshActionPerformed(evt);
            }
        });

        tuteeIdList.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        schedDay.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" }));

        schedTime.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "0800-0930", "0940-1110", "1120-1250", "1300-1430", "1440-1610", "1620-1750" }));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 214, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 362, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel13, javax.swing.GroupLayout.PREFERRED_SIZE, 81, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jLabel12, javax.swing.GroupLayout.PREFERRED_SIZE, 94, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED))
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jLabel8, javax.swing.GroupLayout.DEFAULT_SIZE, 130, Short.MAX_VALUE)
                                .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 130, Short.MAX_VALUE)
                                .addGroup(layout.createSequentialGroup()
                                    .addComponent(jLabel9)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED))))
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(schedLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 167, Short.MAX_VALUE)
                                    .addComponent(courseTxt, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 167, Short.MAX_VALUE)
                                    .addComponent(tutorIdList, 0, 167, Short.MAX_VALUE)
                                    .addComponent(tutorNameLabel, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 167, Short.MAX_VALUE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 34, Short.MAX_VALUE)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel11, javax.swing.GroupLayout.PREFERRED_SIZE, 71, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jLabel10))
                                .addGap(13, 13, 13)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(tuteeIdList, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(tuteeNameLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 161, Short.MAX_VALUE))
                                .addGap(12, 12, 12))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(schedDay, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(BtnUpdate)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(BtnRefresh)
                                        .addGap(7, 7, 7)
                                        .addComponent(BtnCancel))
                                    .addComponent(schedTime, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGap(21, 21, 21)))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 21, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(schedLabel))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel8, javax.swing.GroupLayout.PREFERRED_SIZE, 17, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(tutorIdList, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel10, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel9, javax.swing.GroupLayout.PREFERRED_SIZE, 21, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(tutorNameLabel)))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(tuteeIdList, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel11, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(tuteeNameLabel))))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel12, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(courseTxt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel13, javax.swing.GroupLayout.PREFERRED_SIZE, 21, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(schedDay, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(schedTime, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 13, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(BtnCancel)
                    .addComponent(BtnRefresh)
                    .addComponent(BtnUpdate, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {BtnCancel, BtnRefresh, BtnUpdate});

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void BtnCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BtnCancelActionPerformed
// TODO add your handling code here:
    ViewSched sched = new ViewSched();
    sched.setVisible(true);
    sched.setLocationRelativeTo(null);
    this.dispose();
}//GEN-LAST:event_BtnCancelActionPerformed

private void BtnUpdateActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BtnUpdateActionPerformed
// TODO add your handling code here:
    DBManager con = new DBManager();
    con.Connect();
    
    DBManager con2 = new DBManager();
    con2.Connect();
    
    DBManager con3 = new DBManager();
    con3.Connect();
    
    
    
         if((courseTxt.getText().equals("")))
            {
        
        JOptionPane.showMessageDialog(frame,
        "Please Complete all Fields.",
        "Incomplete",
        JOptionPane.WARNING_MESSAGE);
    }
         
         else{
             
            course = courseTxt.getText();
            QHandler handler = new QHandler();
            QHandler handler2 = new QHandler();
            QHandler handler3 = new QHandler();
            handler.updateSchedInfo(tutorIdList.getSelectedItem().toString(), tutorNameLabel.getText(), 
            tuteeIdList.getSelectedItem().toString(), tuteeNameLabel.getText(), SchedID, schedDay.getSelectedItem().toString(), schedTime.getSelectedItem().toString(), courseTxt.getText());
            con.Push(handler.query);
            con.Disconnect(con.rs);
            handler2.getTutorCourse(tutorIdList.getSelectedItem().toString());
            con2.getInfo(handler2.query);

            try{

            do{ 

                if(con2.rs.getString("Course_courseCode").toString().equals(course)){

                        flag = true;
                }

              }while(con2.rs.next());
            }catch(SQLException ex){}

              if(flag == false){

                handler3.addCourse(tutorIdList.getSelectedItem().toString(), course);
                con3.Push(handler3.query);
                con3.Disconnect(con3.rs);
                }

        con2.Disconnect(con2.rs);
        ViewSched sched = new ViewSched();
        sched.setVisible(true);
        sched.setLocationRelativeTo(null);
        this.dispose();
            }
         
        
}//GEN-LAST:event_BtnUpdateActionPerformed

private void BtnRefreshActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BtnRefreshActionPerformed
// TODO add your handling code here:
     
    refresh();
        
        
}//GEN-LAST:event_BtnRefreshActionPerformed

    private void refresh(){
        
        tutorID = tutorIdList.getSelectedItem().toString();
        tuteeID = tuteeIdList.getSelectedItem().toString();
        
        QHandler queryHandler = new QHandler();
        DBManager dbconnector = new DBManager();
        queryHandler.getTutorInfo(tutorID);
         dbconnector.Connect();
        dbconnector.getInfo(queryHandler.query);
       
        
        
        QHandler queryHandler1 = new QHandler();
        DBManager dbconnector1 = new DBManager();
        queryHandler1.getTuteeInfo(tuteeID);
         dbconnector1.Connect();
        dbconnector1.getInfo(queryHandler1.query);
        
        
        try{
            
        tutorNameLabel.setText(dbconnector.rs.getString("lName") + " " + dbconnector.rs.getString("fName") + " " + dbconnector.rs.getString("mName"));
        tuteeNameLabel.setText(dbconnector1.rs.getString("lName") + " " + dbconnector1.rs.getString("fName") + " " + dbconnector1.rs.getString("mName"));
        }catch(SQLException ex){
        
        }
         dbconnector.Disconnect(dbconnector.rs);
        dbconnector1.Disconnect(dbconnector1.rs);
        
        
    }
    /**
     * @param args the command line arguments
     */
    
    public void Update(){
        
           
       
                
        DBManager dbConnector = new DBManager();
        QHandler qhandler = new QHandler();
        qhandler.getSchedInfo(SchedID);
        dbConnector.Connect();
        dbConnector.getInfo(qhandler.query);
        
        
         try {
             
            schedLabel.setText(SchedID);           
            tutorIdList.setSelectedItem(dbConnector.rs.getString("tutorID"));
            tutorNameLabel.setText(dbConnector.rs.getString("tutorName"));
            tuteeIdList.setSelectedItem(dbConnector.rs.getString("tuteeID"));
            tuteeNameLabel.setText(dbConnector.rs.getString("tuteeName"));
            courseTxt.setText(dbConnector.rs.getString("course"));
         
            
            for(int i=0; i<5; i++){
                if(dbConnector.rs.getString("schedDay").equals(day[i]))
                    schedDay.setSelectedIndex(i);
            }
            
             
            for(int i=0; i<6; i++){
                if(dbConnector.rs.getString("schedTime").equals(time[i]))
                    schedTime.setSelectedIndex(i);
            }
            
            
           
        } catch (SQLException ex) {
            
            
        }
        dbConnector.Disconnect(dbConnector.rs);
        
    }
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(UpdateSched.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(UpdateSched.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(UpdateSched.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(UpdateSched.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new UpdateSched().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton BtnCancel;
    private javax.swing.JButton BtnRefresh;
    private javax.swing.JButton BtnUpdate;
    private javax.swing.JTextField courseTxt;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JComboBox schedDay;
    private javax.swing.JLabel schedLabel;
    private javax.swing.JComboBox schedTime;
    public static javax.swing.JComboBox tuteeIdList;
    public static javax.swing.JLabel tuteeNameLabel;
    public static javax.swing.JComboBox tutorIdList;
    public static javax.swing.JLabel tutorNameLabel;
    // End of variables declaration//GEN-END:variables
}
